import {
  Controller,
  DefaultValuePipe,
  Get,
  ParseArrayPipe,
  ParseBoolPipe,
  ParseEnumPipe,
  ParseFloatPipe,
  ParseIntPipe,
  ParseUUIDPipe,
  Query,
  ValidationPipe,
  Post,
  UsePipes,
  Body,
} from '@nestjs/common';
import { AppService } from './app.service';
import { CreateUserDto } from './dto/app.dto';
import { ApiConsumes } from '@nestjs/swagger';
enum Gender {
  MALE = 'male',
  FEMALE = 'female',
}

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get() // 这里的ParseIntPipe就是一个内置管道
  getHello(@Query('age', ParseIntPipe) age: number): number {
    return age + 10;
  }
  @Get('/float')
  getFloat(@Query('num', ParseFloatPipe) num: number): number {
    return num;
  }
  @Get('/bool')
  getBool(@Query('bl', ParseBoolPipe) bl: boolean): object {
    return { value: bl, type: typeof bl };
  }
  @Get('/array')
  getArray(@Query('arr', ParseArrayPipe) arr: Array<number>): object {
    return { value: arr, type: typeof arr };
  }
  @Get('/uuid')
  parseUUID(@Query('uuid', ParseUUIDPipe) uuid: string): object {
    return { value: uuid, type: typeof uuid };
  }
  @Get('/parseEnum')
  parseEnum(
    @Query('gender', new ParseEnumPipe(Gender)) gender: Gender,
  ): object {
    return { value: gender, type: typeof gender };
  }
  @Get('/defaultValue')
  defaultValue(
    @Query('value', new DefaultValuePipe('hello world')) value: string,
  ): object {
    return { value };
  }
  @Post('/user')
  @ApiConsumes('application/x-www-form-urlencoded')
  @UsePipes(new ValidationPipe()) // 使用ValidationPipe进行数据验证
  createUser(@Body() createUserDto: CreateUserDto) {
    // 在这里处理创建用户的逻辑
    return `User created: ${createUserDto.name}, ${createUserDto.email}`;
  }
}
